﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;



namespace TXTToPNG
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("正在转换，请稍后……");
            string strFilePath = System.IO.Directory.GetCurrentDirectory();
            string[] getFileName = System.IO.Directory.GetFiles((strFilePath + @"\需要转换的文字\"), "*.txt");
            foreach (string strAll in getFileName)
            {
                string str = System.IO.Path.GetFileNameWithoutExtension(strAll);
                ConvertTextFileToImage(strAll, (strFilePath + @"\转换结果\" + str + ".png"));
            }
            Addbackpage();
            Console.WriteLine("转换成功！");
            // Console.ReadLine();
            Console.WriteLine("是否查看生成图片？(Y_or_N)");
            string answer = Console.ReadLine();
            if (answer == "Y")
            {
                System.Diagnostics.Process.Start("explorer", (strFilePath + @"\转换结果\"));
            }


            Console.WriteLine("是否分享到微博？(Y_or_N)");
            string answer1 = Console.ReadLine();
            if (answer1 == "Y")
            {
                System.Diagnostics.Process.Start("http://weibo.com/"); ;
            }
        }

        static void ConvertTextFileToImage(String textFile, String imageFile)
        {
            System.Drawing.Font drawFont = new System.Drawing.Font("微软雅黑", 12);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(1, 1);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            String text = System.IO.File.ReadAllText(textFile, Encoding.GetEncoding("GB2312"));
            System.Drawing.SizeF sf = g.MeasureString(text, drawFont, 440); //设置一个显示的宽度 
            image = new System.Drawing.Bitmap(image, new System.Drawing.Size(Convert.ToInt32(sf.Width), Convert.ToInt32(sf.Height)));
            g = System.Drawing.Graphics.FromImage(image);
            g.Clear(System.Drawing.Color.AliceBlue);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            g.DrawString(text, drawFont, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(new System.Drawing.PointF(0, 0), sf));
            image.Save(imageFile, System.Drawing.Imaging.ImageFormat.Png);
            g.Dispose();
            image.Dispose();
        }
        static void Addbackpage()
        {
            string strFilePath = System.IO.Directory.GetCurrentDirectory();
            string[] getpngName = System.IO.Directory.GetFiles((strFilePath + @"\转换结果\"), "*.png");
           
            foreach (string pngname in getpngName)
            {
                Bitmap source;
                Graphics resultGraphics;    //用来绘图的实例  
                Image img1 = Image.FromFile(pngname);
                Image img2 = Image.FromFile(strFilePath + @"\0.jpg");
                source = new Bitmap(img1.Width, img1.Height + img2.Height);

                resultGraphics = Graphics.FromImage(source); //以source大小来创建一块新的画布  
                resultGraphics.DrawImage(img1, 0, 0, img1.Width, img1.Height);//先把img1画出  
                resultGraphics.DrawImage(img2, 0, img1.Height, img2.Width, img2.Height);//找到img1的位置再把img2画出  
                string[] gettxtName = System.IO.Directory.GetFiles((strFilePath + @"\需要转换的文字\"), "*.txt");
                foreach (string _gettxtname in gettxtName)
                {
                    string str = System.IO.Path.GetFileNameWithoutExtension(_gettxtname);

                    source.Save(strFilePath  +@"\转换结果\"+str + "（加图片版).png");
                }
                resultGraphics.Dispose();

            }
        }
    }
    
}

